Skip to content

Preserve ordering in MultipleChoiceField - #9735

Merged
browniebroke merged 20 commits into
encode:mainfrom
fbozhang:master
Feb 26, 2026
Merged

browniebroke merged 20 commits into
encode:mainfrom
fbozhang:master

Conversation

@fbozhang

@fbozhang fbozhang commented Jul 5, 2025

Copy link
Copy Markdown
Contributor

This modification has the following advantages:

  1. Maintain the input order.
  2. Prevent the output from being a set, which would cause issues with JSON serialization (e.g., when passing parameters to certain third-party SDKs that may internally use json.dumps, and I cannot modify the third party's encoder).

@auvipy auvipy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add unit tests to verify this and make the CI green

@fbozhang

fbozhang commented Jul 5, 2025

Copy link
Copy Markdown
Contributor Author

please also add unit tests to verify this and make the CI green

I’ve added the tests and the CI is green now.

Comment thread tests/test_fields.py Resolved

@browniebroke browniebroke left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems sensible to me 👍🏻

However, it will have to wait for 3.17 as I see this as potentially disruptive for some downstream users. Left a small suggestion

Comment thread tests/test_fields.py Resolved
Comment thread tests/test_fields.py Resolved
Comment thread tests/test_serializer_nested.py Resolved
@browniebroke browniebroke added this to the 3.17 milestone Jul 6, 2025
@fbozhang

fbozhang commented Jul 6, 2025

Copy link
Copy Markdown
Contributor Author

Seems sensible to me 👍🏻

However, it will have to wait for 3.17 as I see this as potentially disruptive for some downstream users. Left a small suggestion

Good point — waiting for 3.17 sounds like the right call. Appreciate the suggestion!

@fbozhang fbozhang closed this Aug 17, 2025
@fbozhang
fbozhang deleted the master branch August 17, 2025 16:37
@fbozhang
fbozhang restored the master branch August 17, 2025 16:39
@fbozhang fbozhang reopened this Aug 17, 2025
@stale

stale Bot commented Oct 18, 2025

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Oct 18, 2025

This comment was marked as outdated.

Comment thread docs/api-guide/fields.md Resolved
Comment thread rest_framework/fields.py Resolved
Comment thread rest_framework/fields.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@browniebroke browniebroke changed the title fix: MultipleChoiceField use ordered sort Preserve ordering in MultipleChoiceField Feb 26, 2026
@browniebroke
browniebroke merged commit a450422 into encode:main Feb 26, 2026
13 checks passed
HosseinSayyedMousavi pushed a commit to HosseinSayyedMousavi/django-rest-framework that referenced this pull request Oct 8, 2026
* fix: MultipleChoiceField use ordered sort

(cherry picked from commit 8436483)

* test: fix unit tests

(cherry picked from commit 6428ac4)

* test: test TestMultipleChoiceField can json serializable

(cherry picked from commit 12908b1)

* test: fix unit test

(cherry picked from commit 73a709c)

* minor: rest old formatting

* fix: using pytest.fail to test

* Update test_fields.py

* Update test_fields.py

* Update test_fields.py

* test: add test cases

* docs: update docs

* Update docs/api-guide/fields.md

* Skip inner list allocation

* Fix punctuation

---------

Co-authored-by: Asif Saif Uddin {"Auvi":"অভি"} <auvipy@gmail.com>
Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>
Co-authored-by: Bruno Alla <alla.brunoo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

4 participants